package com.lww.feature.test.bean.validation;

import org.hibernate.validator.constraints.NotBlank;
import org.hibernate.validator.constraints.Range;
import org.junit.Test;

import javax.validation.*;
import javax.validation.constraints.NotNull;
import javax.validation.executable.ExecutableValidator;
import java.lang.reflect.Method;
import java.util.Set;

/**
 * User: yian
 * Date: 2017/5/5
 */
public class test {
    @Test
    public void test() {

        Order order = new Order();
        order.setStatus("123");
        order.setStatus("created");
        ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
        Validator validator = factory.getValidator();
        Set<ConstraintViolation<Order>> violations = validator.validate(order);

        if(violations.size() == 0) {
        } else {
            for(ConstraintViolation<Order> violation: violations) {
                System.out.println(violation.getMessage());

            }
            }
        }


    @Test
    public void testMethodSimpleParam() throws NoSuchMethodException {
        ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
        ExecutableValidator validator = factory.getValidator().forExecutables();
        Method method = this.getClass().getMethod("简单参数",  Integer.class, String.class, String.class);
        // 校验参数，应该是有两个非法的参数
        Object [] params = new Object[]{100, "", "ReferenceTypeTest"};
        Set<ConstraintViolation<test>> constraintViolations = validator.validateParameters(this, method, params);
        constraintViolations.forEach(p-> System.out.println(p.getMessage()));
        int size = constraintViolations.size();
        System.out.println("非法条数"+size);
    }
    @Test
    public void test参数值含有基本类型() throws NoSuchMethodException {
        ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
        ExecutableValidator validator = factory.getValidator().forExecutables();
        Method method = this.getClass().getMethod("参数值含有基本类型",  int.class, String.class, String.class);
        // 校验参数，应该是有两个非法的参数
        Object [] params = new Object[]{100, "", "ReferenceTypeTest"};
        Set<ConstraintViolation<test>> constraintViolations = validator.validateParameters(this, method, params);
        constraintViolations.forEach(p-> System.out.println(p.getMessage()));
        int size = constraintViolations.size();
        System.out.println("非法条数"+size);
    }
    @Test
    public void testMethodDtoParam() throws NoSuchMethodException {
        ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
        ExecutableValidator validator = factory.getValidator().forExecutables();
        Method method = this.getClass().getMethod("vaildMethodDtoParam",  Integer.class, String.class, Order.class);
        // 校验参数，应该是有两个非法的参数
        Order order = new Order();


        order.setStatus("aaaaaaaaaalk");
        Object [] params = new Object[]{100, "",order};
        Set<ConstraintViolation<test>> constraintViolations = validator.validateParameters(this, method, params);
        constraintViolations.forEach(p-> System.out.println(p.getMessage()));
        int size = constraintViolations.size();
        System.out.println("非法条数"+size);
    }


    // 校验示范方法
    public void 简单参数(@NotNull @Range(min = 0, max = 18)Integer age, @NotBlank String build, String test){}
    public void 参数值含有基本类型(@NotNull @Range(min = 0, max = 18)int age, @NotBlank String build, String test){}
    public void vaildMethodDtoParam(@NotNull @Range(min = 0, max = 18)Integer age, @NotBlank String build, @Valid/*自定义类需要加@Valid才会校验*/ Order test){}
}
